package cn.tedu.ivos.user.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.user.pojo.dto.UserQuery;
import cn.tedu.ivos.user.pojo.dto.UserLoginParam;
import cn.tedu.ivos.user.pojo.dto.UserSaveParam;
import cn.tedu.ivos.user.pojo.vo.UserVO;
import cn.tedu.ivos.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/user")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public JsonResult login(@RequestBody UserLoginParam userLoginParam){
        log.debug("登录操作-入参:{}",userLoginParam);

        log.debug("登录操作-userService:入参:{}",userLoginParam);
        UserVO userVO = userService.login(userLoginParam);
        log.debug("登录操作-userService:入参:{},出参:{}",userLoginParam,userVO);

        JsonResult result = JsonResult.ok(userVO);
        log.debug("登录操作-入参:{},出参:{}",userLoginParam,result);
        return result;
    }

    @PostMapping("/save")
    public JsonResult save(UserSaveParam userSaveParam){
        log.debug("保存用户信息-入参:{}",userSaveParam);
        userService.saveUser(userSaveParam);
        return JsonResult.ok();
    }

    @GetMapping("/select")
    public JsonResult select(UserQuery userQuery){
        log.debug("查询用户信息-入参:{}",userQuery);
        log.debug("查询用户信息-userService:入参:{}",userQuery);
        List<UserVO> userVOList = userService.selectUser(userQuery);
        log.debug("查询用户信息-userService:入参:{},出参:{}",userQuery,userVOList);
        JsonResult result = JsonResult.ok(userVOList);
        log.debug("查询用户信息-入参:{},出参:{}",userQuery,result);
        return result;
    }

    @PostMapping("/update/password/{userId}")
    public JsonResult updatePassword(@PathVariable Long userId){
        log.debug("修改密码-入参:{},{}",userId);
        userService.updatePassword(userId);
        return JsonResult.ok();
    }

    @PostMapping("/update/status/{userId}/{status}")
    public JsonResult updateStatus(@PathVariable Long userId,@PathVariable String status){
        log.debug("修改用户状态-入参:{},{}",userId,status);
        userService.updateStatus(userId,status);
        return JsonResult.ok();
    }

    @PostMapping("/delete/{userId}")
    public JsonResult delete(@PathVariable Long userId){
        log.debug("删除用户-入参:{}",userId);
        userService.deleteUser(userId);
        return JsonResult.ok();
    }
}
